/**
 * 
 */
package email.model;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Dominik Roth
 * Class provides MailServer Data.
 * Singleton-Pattern
 */
public class MailServer {

	private static MailServer mailServer = null;
	private String propertyFile = "settings.properties";
	private Properties prop;
	private String username;
	private char[] password;
	private String smtpUrl;
	private int smtpPort;
	private String imapUrl;
	private int imapPort;
	private int readMailCount;
	
	

	private MailServer()
	{
		prop = new Properties(); 

		try 
		{ 
			prop.load(new FileInputStream(propertyFile));
			
			username = prop.getProperty("user");
			smtpUrl = prop.getProperty("smtpUrl");
			smtpPort = Integer.parseInt(prop.getProperty("smtpPort"));
			imapUrl = prop.getProperty("imapUrl");
			imapPort = Integer.parseInt(prop.getProperty("imapPort"));
			readMailCount = Integer.parseInt(prop.getProperty("readMailCount"));
		} 
		catch (IOException e) 
		{
			this.createProperties();
		}
	}

	/**
	 * Returns an instance of the Mailserver
	 * @return
	 */
	public static MailServer getInstance()
	{
		if(mailServer == null)
			mailServer = new MailServer();
		return mailServer;
	}

	
	/**
	 * Stores the propertier to the prob File
	 */
	public void createProperties()
	{
		FileOutputStream out;
		try {
			out = new FileOutputStream(propertyFile);
			prop.store(out, "---Property File: Mail Server---");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
		prop.setProperty("user", username);
	}

	public char[] getPassword() {
		return password;
	}

	public void setPassword(char[] password) {
		this.password = password;
	}

	public String getSmtpUrl() {
		return smtpUrl;
	}

	public void setSmtpUrl(String smtpUrl) {
		this.smtpUrl = smtpUrl;
		prop.setProperty("smtpUrl", smtpUrl);
	}

	public int getSmtpPort() {
		return smtpPort;
	}

	public void setSmtpPort(int smtpPort) {
		this.smtpPort = smtpPort;
		prop.setProperty("smtpPort", smtpPort + "");
	}

	public String getImapUrl() {
		return imapUrl;
	}

	public void setImapUrl(String imapUrl) {
		this.imapUrl = imapUrl;
		prop.setProperty("imapUrl", imapUrl);
	}

	public int getImapPort() {
		return imapPort;
	}

	public void setImapPort(int imapPort) {
		this.imapPort = imapPort;
		prop.setProperty("imapPort", imapPort + "");
	}

	public int getReadMailCount() {
		return readMailCount;
	}

	public void setReadMailCount(int readMailCount) {
		this.readMailCount = readMailCount;
		prop.setProperty("readMailCount", readMailCount + "");
	}
	
}
